﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Quan_ly_thong_tin_he_thong
{
    public partial class XemSuaNhanVien : Form
    {
        string NgaySinh;
        string tenanh;
        string MSCV;
        string duongdanhinhanh;
        string filename;

        public XemSuaNhanVien()
        {
            InitializeComponent();
        }

        private void khoaThongTin()
        {
            HoTen_NV.Enabled = false;
            NgayComboBox.Enabled = false;
            ThangCombobox.Enabled = false;
            NamCombobox.Enabled = false;
            radioButton1.Checked = false;
            radioButton2.Checked = false;
            CMND_NV.Enabled = false;
            SDT_NV.Enabled = false;
            Email_NV.Enabled = false;
            DiaChi_NV.Enabled = false;
            ChucVuCombobox.Enabled = false;
            chonAnhButton.Enabled = false;
            TenDangNhap_NV.Enabled = false;
            MatKhau_NV.Enabled = false;
            PhongBanCombobox.Enabled = false;
        }

        private void xoaThongTin()
        {
            MaSo_NV.Text = null;
            HoTen_NV.Text = null;
            NgayComboBox.Text = null;
            ThangCombobox.Text = null;
            NamCombobox.Text = null;
            radioButton1.Checked = false;
            radioButton2.Checked = false;
            CMND_NV.Text = null;
            SDT_NV.Text = null;
            Email_NV.Text = null;
            DiaChi_NV.Text = null;
            chonAnhButton.Text = null;
            TenDangNhap_NV.Text = null;
            MatKhau_NV.Text = null;
            AnhNhanVien.Image = null;
        }

        private void QuayLaiButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //private void XoaButton_Click(object sender, EventArgs e)
        //{
        //    ThongBaoXoa f = new ThongBaoXoa();
        //    if (f.ShowDialog() == DialogResult.OK)
        //    {
        //        int i;
        //        xoaNhanVien(MaSo_NV.Text);
        //        loadDataGridView();
        //        lockThongTin();
        //        xoaThongTin();
        //    }
        //    else
        //        f.Close();
        //}

        private void capNhatThongTin(string MaNV, string HoTenNV, string NgaySinh, bool gioiTinhNV, string CMND, string SDT, string Email, string DiaChiNV, string HinhAnhNV, string tenDangNhap, string matKhau, string chucVuNV)
        {
            Config file = new Config(Application.StartupPath + "\\config.ini");
            string connectionstring = file.ReadValue("server", "con");
            SqlConnection conn = new SqlConnection(connectionstring);
            string sql = "update NhanVien set HoTen_NV = @HoTen_NV,NgaySinh_NV = @NgaySinh_NV,GioiTinh_NV = @GioiTinh_NV,CMND_NV = @CMND_NV,SDT_NV = @SDT_NV,Email_NV = @Email_NV,DiaChi_NV = @DiaChi_NV,HinhAnh_NV = @HinhAnh_NV,TenDangNhap_NV = @TenDangNhap_NV,MatKhau_NV = @MatKhau_NV,MaSo_CV = @MaSo_CV where MaSo_NV = @MaSo_NV";
            SqlCommand cmd = new SqlCommand(sql, conn);
            conn.Open();
            cmd.Parameters.AddWithValue("@MaSo_NV", MaNV);
            cmd.Parameters.AddWithValue("@HoTen_NV", HoTenNV);
            cmd.Parameters.AddWithValue("@NgaySinh_NV", NgaySinh);
            cmd.Parameters.AddWithValue("@GioiTinh_NV", gioiTinhNV);
            cmd.Parameters.AddWithValue("@CMND_NV", CMND);
            cmd.Parameters.AddWithValue("@SDT_NV", SDT);
            cmd.Parameters.AddWithValue("@Email_NV", Email);
            cmd.Parameters.AddWithValue("@DiaChi_NV", DiaChiNV);
            cmd.Parameters.AddWithValue("@HinhAnh_NV", HinhAnhNV);
            cmd.Parameters.AddWithValue("@TenDangNhap_NV", tenDangNhap);
            cmd.Parameters.AddWithValue("@MatKhau_NV", matKhau);
            cmd.Parameters.AddWithValue("@MaSo_CV", chucVuNV);
            cmd.ExecuteNonQuery();
            conn.Close();
        }

        private void capNhatThongTinKhongPass(string MaNV, string HoTenNV, string NgaySinh, bool gioiTinhNV, string CMND, string SDT, string Email, string DiaChiNV, string HinhAnhNV, string tenDangNhap, string chucVuNV)
        {
            Config file = new Config(Application.StartupPath + "\\config.ini");
            string connectionstring = file.ReadValue("server", "con");
            SqlConnection conn = new SqlConnection(connectionstring);
            string sql = "update NhanVien set HoTen_NV = @HoTen_NV,NgaySinh_NV = @NgaySinh_NV,GioiTinh_NV = @GioiTinh_NV,CMND_NV = @CMND_NV,SDT_NV = @SDT_NV,Email_NV = @Email_NV,DiaChi_NV = @DiaChi_NV,HinhAnh_NV = @HinhAnh_NV,TenDangNhap_NV = @TenDangNhap_NV,MaSo_CV = @MaSo_CV where MaSo_NV = @MaSo_NV";
            SqlCommand cmd = new SqlCommand(sql, conn);
            conn.Open();
            cmd.Parameters.AddWithValue("@MaSo_NV", MaNV);
            cmd.Parameters.AddWithValue("@HoTen_NV", HoTenNV);
            cmd.Parameters.AddWithValue("@NgaySinh_NV", NgaySinh);
            cmd.Parameters.AddWithValue("@GioiTinh_NV", gioiTinhNV);
            cmd.Parameters.AddWithValue("@CMND_NV", CMND);
            cmd.Parameters.AddWithValue("@SDT_NV", SDT);
            cmd.Parameters.AddWithValue("@Email_NV", Email);
            cmd.Parameters.AddWithValue("@DiaChi_NV", DiaChiNV);
            cmd.Parameters.AddWithValue("@HinhAnh_NV", HinhAnhNV);
            cmd.Parameters.AddWithValue("@TenDangNhap_NV", tenDangNhap);
            cmd.Parameters.AddWithValue("@MaSo_CV", chucVuNV);
            cmd.ExecuteNonQuery();
            conn.Close();
        }

        private void CapNhatButton_Click(object sender, EventArgs e)
        {
            NgaySinh = ThangCombobox.Text + "/" + NgayComboBox.Text + "/" + NamCombobox.Text;
            bool gioitinh;
            if (radioButton1.Checked)
                gioitinh = true;
            else
                gioitinh = false;
            if (MatKhau_NV.Text != "")
            {
                capNhatThongTin(MaSo_NV.Text, HoTen_NV.Text, NgaySinh, gioitinh, CMND_NV.Text, SDT_NV.Text, Email_NV.Text, DiaChi_NV.Text, tenanh, TenDangNhap_NV.Text, MatKhau_NV.Text, ChucVuCombobox.SelectedValue.ToString());
                loadDataGridViewWithMSCV("");
                thongBaoLabel.Text = "Cập nhật thành công !";
                khoaThongTin();
                xoaThongTin();
            }
            else
            {
                capNhatThongTinKhongPass(MaSo_NV.Text, HoTen_NV.Text, NgaySinh, gioitinh, CMND_NV.Text, SDT_NV.Text, Email_NV.Text, DiaChi_NV.Text, tenanh, TenDangNhap_NV.Text, ChucVuCombobox.SelectedValue.ToString());
                loadDataGridViewWithMSCV("");
                thongBaoLabel.Text = "Cập nhật thành công !";
                khoaThongTin();
                xoaThongTin();
            }
        }

        private void ChinhSuaButton_Click(object sender, EventArgs e)
        {
            HoTen_NV.Enabled = true;
            NgayComboBox.Enabled = true;
            ThangCombobox.Enabled = true;
            NamCombobox.Enabled = true;
            CMND_NV.Enabled = true;
            SDT_NV.Enabled = true;
            Email_NV.Enabled = true;
            DiaChi_NV.Enabled = true;
            ChucVuCombobox.Enabled = true;
            TenDangNhap_NV.Enabled = true;
            MatKhau_NV.Enabled = true;
            chonAnhButton.Enabled = true;
            PhongBanCombobox.Enabled = true;
        }

        public void loadDataGridViewWithMSCV(string MSCV)
        {
            Config file = new Config(Application.StartupPath + "\\Config.ini");
            string connectionstring = file.ReadValue("server", "con");
            SqlConnection conn = new SqlConnection(connectionstring);
            //string sql = "select * from NhanVien, ChucVu where NhanVien.MaSo_CV = ChucVu.MaSo_CV";
            if (MSCV == "")
            {
                string sql = "select *,(case when GioiTinh_NV = '1' then 'Nam' else N'Nữ' end)AS GioiTinh from NhanVien";
                SqlCommand cmd = new SqlCommand(sql, conn);
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                adapter.Fill(ds, "NhanVien");
                dataGridView1.AutoGenerateColumns = false;
                dataGridView1.DataSource = ds.Tables["NhanVien"].DefaultView;
            }
            else
            {
                string sql = "select *,(case when GioiTinh_NV = '1' then 'Nam' else N'Nữ' end)AS GioiTinh from NhanVien where MaSo_NV = @MaSo_NV";
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.Parameters.AddWithValue("@MaSo_NV", MSCV);
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                adapter.Fill(ds, "NhanVien");
                dataGridView1.AutoGenerateColumns = false;
                dataGridView1.DataSource = ds.Tables["NhanVien"].DefaultView;
            }
        }

        private void XemSuaNhanVien_Load(object sender, EventArgs e)
        {
            loadDataGridViewWithMSCV("");
            loadComboboxPhongBan();
        }

        private void TimKiemButton_Click(object sender, EventArgs e)
        {
            timKiemNV();
        }

        private void xoaNhanVien(string MS_NV)
        {
            Config file = new Config(Application.StartupPath + "\\Config.ini");
            string connectionstring = file.ReadValue("server", "con");
            SqlConnection conn = new SqlConnection(connectionstring);
            string sql = "delete from NhanVien where MaSo_NV = @MaSo_NV";
            SqlCommand cmd = new SqlCommand(sql, conn);
            conn.Open();
            cmd.Parameters.AddWithValue("@MaSo_NV", MS_NV);
            cmd.ExecuteNonQuery();
            conn.Close();
        }

        private void docThongTinNV(string MS_NV)
        {
            Config file = new Config(Application.StartupPath + "\\config.ini");
            string connectionstring = file.ReadValue("server", "con");
            SqlConnection conn = new SqlConnection(connectionstring);
            string sql = "select *,convert(char(10),NgaySinh_NV,103) as NgaySinh from NhanVien where MaSo_NV = @MaSo_NV";
            SqlCommand cmd = new SqlCommand(sql, conn);
            conn.Open();
            cmd.Parameters.AddWithValue("@MaSo_NV", MS_NV);
            SqlDataReader reader = cmd.ExecuteReader();
            if (reader.Read())
            {
                MaSo_NV.Text = reader["MaSo_NV"].ToString();
                HoTen_NV.Text = reader["HoTen_NV"].ToString();
                NgayComboBox.Text = ((string)reader["NgaySinh"]).Split('/')[0];
                ThangCombobox.Text = ((string)reader["NgaySinh"]).Split('/')[1];
                NamCombobox.Text = ((string)reader["NgaySinh"]).Split('/')[2];
                if ((bool)reader["GioiTinh_NV"] == true)
                    radioButton1.Checked = true;
                else
                    radioButton2.Checked = true;
                CMND_NV.Text = reader["CMND_NV"].ToString();
                SDT_NV.Text = reader["SDT_NV"].ToString();
                Email_NV.Text = reader["Email_NV"].ToString();
                DiaChi_NV.Text = reader["DiaChi_NV"].ToString();
                string duongdan = file.ReadValue("duongdanhinhanhnhanvien", "HinhAnhNhanVien");
                tenanh = reader["HinhAnh_NV"].ToString();
                duongdanhinhanh = duongdan + tenanh;
                AnhNhanVien.Image = Image.FromFile(duongdanhinhanh);
                TenDangNhap_NV.Text = reader["TenDangNhap_NV"].ToString();
                MSCV = reader["MaSo_CV"].ToString();
            }
            reader.Close();
            cmd.CommandText = "select PhongBan.MaSo_PB,Ten_PB,Ten_CV,MaSo_CV from ChucVu join PhongBan on ChucVu.MaSo_PB = PhongBan.MaSo_PB where MaSo_CV = @MaSo_CV";
            cmd.Parameters.Clear();
            cmd.Parameters.AddWithValue("MaSo_CV", MSCV);
            reader = cmd.ExecuteReader();
            if (reader.Read())
            {
                PhongBanCombobox.Text = (string)reader["Ten_PB"];
                PhongBanCombobox.SelectedValue = (string)reader["MaSo_PB"];
                loadcomboboxChucVu(PhongBanCombobox.SelectedValue.ToString());
                ChucVuCombobox.Text = (string)reader["Ten_CV"];
                ChucVuCombobox.SelectedValue = (string)reader["MaSo_CV"];
            }
            reader.Close();
            conn.Close();
        }

        private void timKiemNV()
        {
            Config file = new Config(Application.StartupPath + "\\Config.ini");
            string connectionstring = file.ReadValue("server", "con");
            SqlConnection conn = new SqlConnection(connectionstring);
            if (comboboxTimKiem.Text == "Tất cả")
            {

                string sql = "select *,(case when GioiTinh_NV = '1' then 'Nam' else N'Nữ' end)AS GioiTinh from NhanVien";
                SqlCommand cmd = new SqlCommand(sql, conn);
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                adapter.Fill(ds, "NhanVien");
                dataGridView1.AutoGenerateColumns = false;
                dataGridView1.DataSource = ds.Tables["NhanVien"].DefaultView;
            }
            else
                if (comboboxTimKiem.Text == "Mã nhân viên")
                {
                    string sql = "select *,(case when GioiTinh_NV = '1' then 'Nam' else N'Nữ' end) AS GioiTinh from NhanVien where MaSo_NV = @MaSo_NV";
                    SqlCommand cmd = new SqlCommand(sql, conn);
                    cmd.Parameters.AddWithValue("@MaSo_NV", txtTimKiem.Text);
                    SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                    DataSet ds = new DataSet();
                    adapter.Fill(ds, "NhanVien");
                    dataGridView1.AutoGenerateColumns = false;
                    dataGridView1.DataSource = ds.Tables["NhanVien"].DefaultView;
                }
            else
                    if (comboboxTimKiem.Text == "Họ tên nhân viên")
                    {
                        string sql = "select *,(case when GioiTinh_NV = '1' then 'Nam' else N'Nữ' end)AS GioiTinh from NhanVien where HoTen_NV like '"+txtTimKiem.Text+"%'";
                        SqlCommand cmd = new SqlCommand(sql, conn);
                        //cmd.Parameters.AddWithValue("@HoTen_NV", txtTimKiem.Text);
                        SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                        DataSet ds = new DataSet();
                        adapter.Fill(ds, "NhanVien");
                        dataGridView1.AutoGenerateColumns = false;
                        dataGridView1.DataSource = ds.Tables["NhanVien"].DefaultView;
                    }
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            string MSNV;
            MSNV = dataGridView1.Rows[e.RowIndex].Cells["MaSo_NV1"].Value.ToString();
            docThongTinNV(MSNV);
        }

        private void chonAnhButton_Click(object sender, EventArgs e)
        {
            OpenFileDialog f = new OpenFileDialog();
            Config file = new Config(Application.StartupPath + "\\config.ini");
            string anhNhanVIen = file.ReadValue("duongdanhinhanhnhanvien", "HinhAnhNhanVien");
            f.InitialDirectory = anhNhanVIen;
            f.Filter = "Images Files (*.bmp, *jpg, *.png)|*.bmp; *.jpg; *.png|All Files (*.*)|*.*";
            if (f.ShowDialog() == DialogResult.OK)
            {
                filename = f.FileName;
                tenanh = System.IO.Path.GetFileName(f.FileName);
                AnhNhanVien.Image = Image.FromFile(filename);
            }
        }

        private void comboboxTimKiem_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtTimKiem.Text = null;
        }

        private void loadcomboboxChucVu(string MSPB)
        {
            Config file = new Config(Application.StartupPath + "\\Config.ini");
            string connectionstring = file.ReadValue("server", "con");
            SqlConnection conn = new SqlConnection(connectionstring);
            string sql = "select * from ChucVu where MaSo_PB = @MaSo_PB";
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.Parameters.AddWithValue("@MaSo_PB", MSPB);
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            adapter.Fill(ds, "ChucVu");
            ChucVuCombobox.DisplayMember = "Ten_CV";
            ChucVuCombobox.ValueMember = "MaSo_CV";
            ChucVuCombobox.DataSource = ds.Tables["ChucVu"].DefaultView;
        }

        private void loadComboboxPhongBan()
        {
            Config file = new Config(Application.StartupPath + "\\Config.ini");
            string connectionstring = file.ReadValue("server", "con");
            SqlConnection conn = new SqlConnection(connectionstring);
            string sql = "select * from PhongBan";
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            adapter.Fill(ds, "PhongBan");
            PhongBanCombobox.DisplayMember = "Ten_PB";
            PhongBanCombobox.ValueMember = "MaSo_PB";
            PhongBanCombobox.DataSource = ds.Tables["PhongBan"].DefaultView;
        }

        private void PhongBanCombobox_SelectedIndexChanged(object sender, EventArgs e)
        {
            loadcomboboxChucVu(PhongBanCombobox.SelectedValue.ToString());
        }

        private void XoaButton_Click(object sender, EventArgs e)
        {
            xoaNhanVien(MaSo_NV.Text);
            loadDataGridViewWithMSCV("");
            xoaThongTin();
            khoaThongTin();
        }
    }
}
